package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        // 设置分页参数
        PageHelper.startPage(page, pageSize);

        // 执行查询
        List<Emp> list = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) list;

        // 封装pageBean对象
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void del(List<Integer> ids) {
        empMapper.del(ids);

    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 交给spring事务
    public void sava(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());

        empMapper.sava(emp);


    }

    @Override
    public Emp getById(Integer id) {
        Emp emp = empMapper.getById(id);

        return emp;
    }

    @Override
    public Emp edit(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.edit(emp);
        return emp;
    }

    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.getUsernameAndPassword(emp);

        return e;
    }
}
